
const con = require('../../config/const')
const isObject = require('lodash/isObject')
const isNull = require('lodash/isNull')
const isUndefined = require('lodash/isUndefined')

// 所有功能类的基类
module.exports = class Base
{
    //用于api接口,根据res,生成对应的数组
    auto_return(res, successInfo = '成功进行操作', failureInfo = '没有数据返回')
    {
        if (res) {
            return this.auto_success(res, successInfo);
        } else {
            return this.null_data(res, failureInfo);
        }
    }

    //自动设置时间异常状态
    time_err(res, failureInfo = '时间异常')
    {
        return {status: con.TIME_ERR_STATUS, res: this._auto_res(res), msg: failureInfo, code: con.TIME_ERR_CODE}
    }

    //自动设置通用错误状态
    auto_failure(res, failureInfo = '通用错误', failureStatus = con.ERR_STATUS, failureCode = con.ERR_CODE)
    {
        return {status: failureStatus, res: this._auto_res(res), msg: failureInfo, code: failureCode}
    }

    //自动设置服务器异常状态
    server_err(res, failureInfo = '服务器异常')
    {
        return {status: con.SERVER_ERR_STATUS, res: this._auto_res(res), msg: failureInfo, code: con.SERVER_ERR_CODE}
    }

    //自动设置验证失败状态
    verify_err(res, failureInfo = '验证失败')
    {
        return {status: con.VERIFY_ERR_STATUS, res: this._auto_res(res), msg: failureInfo, code: con.VERIFY_ERR_CODE}
    }

    //自动设置需要登陆状态
    need_login(res = null, failureInfo = '请登陆后操作')
    {
        return {status: con.NEED_LOGIN_STATUS, res: this._auto_res(res), msg: failureInfo, code: con.NEED_LOGIN_CODE}
    }

    //自动设置参数错误失败状态
    param_err(res, failureInfo = '进行操作失败')
    {
        return {status: con.PARAM_ERR_STATUS, res: this._auto_res(res), msg: failureInfo, code: con.PARAM_ERR_CODE}
    }

    //自动设置没有数据状态
    null_data(res, failureInfo = '没有数据返回')
    {
        return {status: con.NULL_DATA_STATUS, res: this._auto_res(res), msg: failureInfo, code: con.NULL_DATA_CODE}
    }

    //自动设置成功状态
    auto_success(res, successInfo = '成功进行操作')
    {
        return {status: con.SUCCESS_STATUS, res: this._auto_res(res), msg: successInfo, code: con.SUCCESS_CODE}
    }

    //把非空非对象 -> 转换成对象
    _auto_res(res)
    {
        if (isNull(res) || isUndefined(res)) {
            res = {autoRes: ''}
        } else if (!isObject(res)) {
            res = {autoRes: res}
        }
        return res;
    }
}
